package helpers;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import model.RealEstate;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class CrawlerOloHelper {
	private int step = 30;
	
	public static RealEstate getContent(String url) {
		RealEstate re = new RealEstate();
		try {
			Document doc = Jsoup.connect(url).get();
			if (doc.select(".main_right .content_dt") != null) {
				Elements contents = doc.select(".main_right .content_dt");
				//Get content
				String title = StringHelper.html2text(contents.select("#aTitle").html());
				String alias = StringHelper.replaceVietnamese(title);
				String price = StringHelper.html2text(contents.select("h2").eq(1).html());
				price = StringHelper.findString("[^:]*", price).get(2);
				Elements desc = contents.select("p");
				String description = "";
				for (int i = 0; i < desc.size(); i++) {
					if (!desc.get(i).hasClass("address")) {
						description += desc.get(i).html();
					}
				}
				description = StringHelper.html2text(description);
				
				String district = StringHelper.html2text(contents.select("p.address a").get(0).html());
				String province = StringHelper.html2text(contents.select("p.address a").get(1).html());
				String project = "";
				if (contents.select("p.address a").size() > 2) {
					project = StringHelper.html2text(contents.select("p.address a").get(2).html());
				}
				
				contents.select("p.address a").remove();
				String address = StringHelper.html2text(contents.select("p.address b").first().html().replace(" -", "")); 
				
				String infoDate = doc.select(".main_right .top_dt .infodate").html();
				String REGEX = "\\b\\d\\d-\\d\\d-\\d\\d\\d\\d\\b";
				ArrayList<String> dates = StringHelper.findString(REGEX, infoDate);
				Date date = new Date();				
				if (dates.size() > 0) {
					date = DateHelper.setDate(dates.get(0) + " 0:0:0", null);
				}
				
				Elements contact = contents.select("ul.list-contact");
				contact.select("li b").remove();
				String contactName = StringHelper.html2text(contact.select("li").get(1).html());
				String contactPhone = contact.select("li").get(2).html();
				
				String typeService = StringHelper.html2text(doc.select(".main_right .top_dt .title_dt").select("h3").html());
				String typeRealEstate = StringHelper.html2text(doc.select(".main_right .top_dt .title_dt").select("span a").html());
				
				//Set attribute
				UUID id = UUID.randomUUID();
				re.setId(id.toString());
				re.setTitle(title);
				re.setAlias(alias);
				re.setPrice(price);
				re.setDescription(description);
				re.setAddress(address);
				re.setDistrict(district);
				re.setProvince(province);
				re.setContactName(contactName);
				re.setContactPhone(contactPhone);
				re.setDate(date);
				re.setTypeService(typeService);
				re.setTypeRealEstate(typeRealEstate);
				re.setUrl(url);
				re.setProject(project);
			} else {
				System.out.println("This url: " + url + " has not content!");
			}
			//System.out.println(doc);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return re;
	}
	
	public static ArrayList<String> getLink(String url) {
		ArrayList<String> listLink = new ArrayList<String>();
		try {
			Document doc = Jsoup.connect(url).get();
			if (doc.select("#announce .hover").size() > 0) {
				Elements items = doc.select("#announce .hover");
				String href = "";
				for (int i = 0; i < items.size(); i++) {
					href = "http://www.olo.vn"  + items.get(i).select(".title a").attr("href");
					listLink.add(href);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listLink;
	}
}
